package top.jandmla.excel.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import top.jandmla.excel.pojo.QueryPageDTO;
import top.jandmla.excel.bean.Name;
import top.jandmla.excel.bean.Test;
import top.jandmla.excel.dao.NameMapper;
import top.jandmla.excel.service.NameService;
import top.jandmla.excel.service.TestService;
import top.jandmla.excel.util.ExcelRead;
import top.jandmla.excel.util.FileUpload;
import top.jandmla.excel.util.excelReadImpl.Excel07Read;
import top.jandmla.excel.util.excelReadImpl.Recover;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

@Service
public class NameServiceImpl extends ServiceImpl<NameMapper, Name> implements NameService {

    @Resource
    private NameMapper nameMapper;

    @Autowired
    private TestService testService;

    /**
     * 根据上传的文件类容，来批量插入数据
     *
     * @param mfile 上传的文件对象
     * @return 插入结果
     */
    @Override
    public boolean insertFile(MultipartFile mfile) {
        //判断文件为空
        if (mfile.isEmpty()) {
            return false;
        }

        //转换文件类型
        String name = mfile.getOriginalFilename();
        File file = FileUpload.getFileByMultipartFile(mfile);

        List<Test> list;
        ExcelRead recover;

//        根据文件的类型，来处理文件名和来选择使用不同的类来处理文件
        assert name != null;
        assert file != null;
        if (name.endsWith(".xls")) {
            recover = new Recover();
            name = file.getName().replace(".xls", "");
        } else if (name.endsWith("xlsx")) {
            recover = new Excel07Read();
            name = file.getName().replace(".xlsx", "");
        } else {
            return false;
        }

//        插入文件名称
        QueryWrapper<Name> wrapper = new QueryWrapper<>();
        Name name1 = new Name();
        name1.setName(name);
        int insertCount = nameMapper.insert(name1);
        if (insertCount == 0)
            return false;

//        插入文件数据
        QueryWrapper<Test> wrapper1 = new QueryWrapper<>();
        wrapper.select("max(id)");
        list = recover.getTestFromSheet(file, name1.getId(), "");
        if (list != null) {
            return testService.saveBatch(list);
        } else
            return false;
    }

    @Override
    public IPage<Name> selectPageAndName(QueryPageDTO<Name> dto) {

        //不存在条件则全部查询
        QueryWrapper<Name> wrapper = null;
        Name name = dto.getQuery();

        //存在ID则直接按照ID查询
        if (!StringUtils.isEmpty(name.getId())) {
            wrapper = new QueryWrapper<>();
            wrapper.eq("id", name.getId());

            //不存在ID，存在名称，则按照名称模糊查询
        } else if (!StringUtils.isEmpty(name.getName())) {
            wrapper = new QueryWrapper<>();
            wrapper.like("name", name.getName());
        }

        return nameMapper.selectPage(dto.getPage(), wrapper);
    }
}
